package com.ken.mqbase.base;

import com.ken.constant.RabbitConstant;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * 使用原jar包，最基础一个测试列子（消费者）
 * Created by renchongyu on 2018/11/12.
 */
public class Producer {

    public static void main(String[] args) throws Exception{

        //1.创建一个ConnectionFactory
        ConnectionFactory conn = new ConnectionFactory();
        conn.setHost(RabbitConstant.RABBITMQ_IP);
        conn.setPort(RabbitConstant.RABBITMQ_PORT);
        conn.setUsername(RabbitConstant.RABBITMQ_USER_NAME);
        conn.setPassword(RabbitConstant.RABBITMQ_USER_PASSWORD);
        conn.setVirtualHost(RabbitConstant.RABBITMQ_VIRTUAL_HOST);

        //2.通过连接工厂连接
        Connection connection = conn.newConnection();

        //3.通过连接创建一个channel
        Channel channel = connection.createChannel();

        //4.通过channel发送一条消息
        String msg = "Hello RabbitMQ! from Ken's";
        /**
         * exchange路由器
         * routingKey key键
         * props 配置
         * body 实际消息
         */
        for(int i=0;i<5;i++){
            channel.basicPublish("","test001",null,msg.getBytes());
        }

        //5.关闭相关连接
        channel.close();
        connection.close();

    }
}
